package com.paas.form.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.paas.common.cache.CacheKey;
import com.paas.common.util.BeanUtil;
import com.paas.common.web.service.GenericCacheService;
import com.paas.form.entity.FormAttributeEntity;
import com.paas.form.entity.FormViewPubEntity;
import org.springframework.stereotype.Service;

import com.paas.form.dao.FormAttributePubDao;
import com.paas.form.entity.FormAttributePubEntity;
import com.paas.form.service.IFormAttributePubService;
import com.paas.common.web.service.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

@Service("formAttributePubService")
public class FormAttributePubServiceImpl  extends GenericCacheService<FormAttributePubDao,FormAttributePubEntity> implements IFormAttributePubService{



    @Override
    public List<FormAttributePubEntity>  getValidListByViewId(String viewId){
        //定义查询构建器
        QueryWrapper<FormAttributePubEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("view_id",viewId);
        queryWrapper.eq("status","1");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<FormAttributePubEntity> getListByViewIds(List<String> viewIds) {
        //定义查询构建器
        QueryWrapper<FormAttributePubEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("view_id",viewIds);
        queryWrapper.eq("status","1");
        return this.baseMapper.selectList(queryWrapper);
    }

//    @Override
//    public List<FormAttributePubEntity> getListByViewIdsWithCache(List<String> viewIds) {
//        if(BeanUtil.isEmpty(viewIds)){
//            return null;
//        }
//        List<FormAttributePubEntity> allAttributeList = new ArrayList<>();
//
//        for(String viewId:viewIds){
//            //从缓存中获取单个视图属性
//            List<FormAttributePubEntity> formAttributePubList =( List<FormAttributePubEntity>)this.getCacheByKey(viewId);
//            if(formAttributePubList ==null){
//                formAttributePubList =getValidListByViewId(viewId);
//                //添加数据至缓存
//                if(BeanUtil.isNotEmpty(formAttributePubList) && formAttributePubList.size()>0){
//                    this.addCache(viewId,formAttributePubList,2 * 60 * 60);
//                }else{
//                    formAttributePubList = new ArrayList<>();
//                    this.addCache(viewId,formAttributePubList,2 * 60 * 60);
//                }
//
//            }
//            allAttributeList.addAll(formAttributePubList);
//        }
//        return allAttributeList;
//    }

    @Override
    public void delAttrCacheByViewIds(List<String> viewIds){
        for(String viewId:viewIds){
            this.deleteCacheByKey(viewId);
        }
    }


    @Override
    protected CacheKey buildCacheKey() {
        return new CacheKey("FORM_VIEWPUB_ATTR_INFO","定版视图属性信息");
    }
}